//
//  PVDDetailChecklistScanCell.swift
//  ezyform
//
//  Created by Moennig Q on 5/9/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
import RxCocoa
import RxSwift
class PVDDetailChecklistScanCell: UITableViewCell {
    fileprivate lazy var disposeBag = DisposeBag()
    
    lazy var backView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    let typeImageView = UIImageView()
    let titleLabel = ContentLabel(R.font.interSemiBold(size: 16))
    var resultLabel = ContentLabel(R.font.interMedium(size: 14),hexStr: "#63666AFF")
    let moreArrowImageView = UIImageView(image: UIImage(named: "pvdlist_icon_more"))
    
    lazy var uploadFlagImageView: UIImageView = {
        let image = UIImageView()
        return image
    }()
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpviews()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    func setUpviews(){
        self.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        self.contentView.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        self.selectionStyle = .none
        
        //back view
        self.contentView.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(BaseChecklistTableView.checklistCellHeight)
            make.bottom.equalToSuperview()
        }
        //  image view
        typeImageView.image = R.image.pvdDetail_panel_scan()
        backView.addSubview(typeImageView)
        typeImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(16)
            make.width.height.equalTo(30)
        }
        //uploadFlagImageView,是否上传成功的标记，红色感叹号是本地数据没有上传，绿色勾表示上传成功
        backView.addSubview(uploadFlagImageView)
        uploadFlagImageView.snp.makeConstraints { (make) in
            make.bottom.equalTo(typeImageView)
            make.right.equalTo(typeImageView).offset(3)
            make.size.equalTo(CGSize(width: 16, height: 11))
        }
        //isRequiredImageView
        let isRequiredImageView = UIImageView(image: UIImage(named: "checkListCellStar"))
        backView.addSubview(isRequiredImageView)
        isRequiredImageView.snp.makeConstraints { make in
            make.top.equalTo(typeImageView).offset(6)
            make.left.equalTo(typeImageView.snp.right).offset(4)
            make.height.width.equalTo(7)
        }
        //title
        backView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(typeImageView.snp.right).offset(12)
            make.height.width.greaterThanOrEqualTo(0)
            make.centerY.equalTo(typeImageView)
        }
        //more arrow
        moreArrowImageView.contentMode = .center
        backView.addSubview(moreArrowImageView)
        moreArrowImageView.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.size.equalTo(24)
            make.centerY.equalTo(typeImageView)
        }
        //result
        backView.addSubview(resultLabel)
        resultLabel.snp.makeConstraints { make in
            make.right.equalTo(moreArrowImageView.snp.left)
            make.width.height.greaterThanOrEqualTo(0)
            make.centerY.equalTo(typeImageView)
        }
    }
    func setData(_ checkModel: PVDChecklistModel){
        self.titleLabel.text = checkModel.title
        if checkModel.id == ChecklistFixedId.panels.rawValue{//panels
            backView.drawCorner(byRoundingCorners: [.topLeft,.topRight])
            let finishCount = JobDetailModelHelper.shared.jobDetailModel.pvd_serial_numbers.count
            //计划数量
            let planCount: Int = JobDetailModelHelper.shared.jobDetailModel.system_detail.panel_plant_count
            if planCount > 0 {//有计划数量
                self.setMinBarcodeCount(finishCount, plantCnt: planCount)
            }else{//没有计划数量
                self.resultLabel.text = (finishCount > 0 ? "#\(finishCount)" : "")
            }
            //设置是否有本地数据未上传
            self.uploadFlagImageView.image = (JobDetailModelHelper.shared.jobDetailModel.panelNumbersModifyFlag ? R.image.uploadImageFailed() : nil)
        }else {
            if checkModel.id == ChecklistFixedId.inverters.rawValue{//inverters
                backView.drawCorner(byRoundingCorners: [.bottomLeft,.bottomRight])
            }else if checkModel.id == ChecklistFixedId.tanks.rawValue{//tanks
                backView.drawCorner(byRoundingCorners: .allCorners)
            }
            let finishCount =  JobDetailModelHelper.shared.jobDetailModel.pvd_inverter_serial_numbers.count
            //计划数量
            let planCount: Int = JobDetailModelHelper.shared.jobDetailModel.system_detail.inverter_plant_count
            if planCount > 0 {//有计划数量
                self.setMinBarcodeCount(finishCount, plantCnt: planCount)
            }else{//没有计划数量
                self.resultLabel.text = (finishCount > 0 ? "#\(finishCount)" : "")
            }
            //设置是否有本地数据未上传
            self.uploadFlagImageView.image = (JobDetailModelHelper.shared.jobDetailModel.inverterNumbersModifyFlag ? R.image.uploadImageFailed() : nil)
        }
    }
    //MARK: - 设置有计划数量的样式
    private func setMinBarcodeCount(_ addedCnt: Int, plantCnt: Int){
        let subStr = "\(addedCnt)"
        let content = subStr + "/\(plantCnt)"
        let attrString = NSMutableAttributedString(string: content )
        let attr1: [NSMutableAttributedString.Key: Any] = [.foregroundColor: UIColor(hexStr: "#63666A")]
        let attr2: [NSMutableAttributedString.Key: Any] = [.foregroundColor: UIColor(hexStr: "#379657")]
        
        attrString.addAttributes(attr1, range: NSRange(location: 0, length: attrString.length))
        attrString.addAttributes(attr2, range: NSRange(location: 0, length: subStr.count))
        self.resultLabel.attributedText = attrString
    }
}
